import service from '../utils/axios'
import type { ApiResponse } from '../utils/axios'

// 课程数据接口
export interface CourseInfo {
  id: number
  name: string
  teacherId: number
  teacherIds?: number[]
  collegeId?: number
  courseCode: string
  createBy?: string
  createTime?: string
  updateBy?: string
  updateTime?: string
}

// 分页查询参数接口
interface CourseQueryParams {
  name?: string
  teacherId?: number
  collegeId?: number
  page?: number
  size?: number
}

// 分页查询响应接口
interface PageResult<T> {
  data: T[]
  total: number
  page: number
  size: number
}

// 创建/更新课程请求接口
interface CourseRequest {
  name: string
  teacherId?: number
  teacherIds?: number[]
  collegeId?: number
  courseCode: string
}

/**
 * 获取课程列表（分页）
 */
export function getCourseList(data: CourseQueryParams): Promise<ApiResponse<PageResult<CourseInfo>>> {
  return service({
    url: '/api/courses/query',
    method: 'post',
    data
  })
}

/**
 * 获取当前教师的课程列表
 */
export function getMyCourses(): Promise<ApiResponse<CourseInfo[]>> {
  return service({
    url: '/api/courses/my',
    method: 'get'
  })
}

/**
 * 根据学院ID获取课程列表
 */
export function getCoursesByCollege(collegeId: number): Promise<ApiResponse<CourseInfo[]>> {
  return service({
    url: `/api/courses/college/${collegeId}`,
    method: 'get'
  })
}

/**
 * 获取课程详情
 */
export function getCourseById(id: number): Promise<ApiResponse<CourseInfo>> {
  return service({
    url: `/api/courses/${id}`,
    method: 'get'
  })
}

/**
 * 创建课程
 */
export function createCourse(data: CourseRequest): Promise<ApiResponse<{ id: number }>> {
  return service({
    url: '/api/courses',
    method: 'post',
    data
  })
}

/**
 * 更新课程
 */
export function updateCourse(id: number, data: CourseRequest): Promise<ApiResponse<void>> {
  return service({
    url: `/api/courses/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除课程
 */
export function deleteCourse(id: number): Promise<ApiResponse<void>> {
  return service({
    url: `/api/courses/${id}`,
    method: 'delete'
  })
}

/**
 * 学生选课
 */
export function selectCourse(courseId: number): Promise<ApiResponse<number>> {
  return service({
    url: `/api/student-courses/select/${courseId}`,
    method: 'post'
  })
}

/**
 * 学生退课
 */
export function unselectCourse(courseId: number): Promise<ApiResponse<void>> {
  return service({
    url: `/api/student-courses/unselect/${courseId}`,
    method: 'delete'
  })
}

/**
 * 检查学生是否已选该课程
 */
export function checkCourseSelected(courseId: number): Promise<ApiResponse<boolean>> {
  return service({
    url: `/api/student-courses/check/${courseId}`,
    method: 'get'
  })
}

/**
 * 获取学生已选课程列表
 */
export function getMySelectedCourses(): Promise<ApiResponse<CourseInfo[]>> {
  return service({
    url: '/api/student-courses/my',
    method: 'get'
  })
}

/**
 * 获取选择某课程的学生列表（教师和管理员）
 */
export function getCourseStudents(courseId: number): Promise<ApiResponse<UserInfo[]>> {
  return service({
    url: `/api/student-courses/course/${courseId}/students`,
    method: 'get'
  })
}

// 用户信息接口（用于选课学生列表）
export interface UserInfo {
  id: number
  username: string
  realName: string
  roleType: string
  sex?: string
  email: string
  phonenumber?: string
  collegeId?: number
  classId?: number
}

